package unrn.isiii.service.ws;

import java.io.StringReader;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonReaderFactory;
import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;

import unrn.isiii.bean.rest.Mensaje;
import unrn.isiii.model.PojoTraking;

public class Message {
	/** log */
	private static final Logger logger = Logger.getLogger("Message");

	public static class MessageEncoder implements Encoder.Text<Message> {

		@Override
		public void init(final EndpointConfig config) {
		}

		@Override
		public String encode(final Message message) throws EncodeException {
			return Json.createObjectBuilder()
					.add("username", message.getUsername())
					.add("message", message.getMessage())
					.add("linea", message.getLinea())
					.add("lat", message.getLat()).add("lng", message.getLng())
					.build().toString();
		}

		@Override
		public void destroy() {
		}
	}

	public static class MessageDecoder implements Decoder.Text<Message> {
		private JsonReaderFactory factory = Json
				.createReaderFactory(Collections.<String, Object> emptyMap());

		@Override
		public void init(final EndpointConfig config) {
		}

		@Override
		public Message decode(final String str) throws DecodeException {
			final Message message = new Message();

			try (final JsonReader reader = factory
					.createReader(new StringReader(str))) {
				final JsonObject json = reader.readObject();
				message.setUsername(json.getString("username"));
				message.setMessage(json.getString("message"));
				message.setLinea(json.getString("linea"));
				message.setLat(Double.parseDouble(json.getString("lat")));
				message.setLng(Double.parseDouble(json.getString("lng")));
			} catch (Exception e) {
				logger.log(Level.WARNING, " decode", e.getMessage());
			}

			return message;
		}

		@Override
		public boolean willDecode(final String str) {
			return true;
		}

		@Override
		public void destroy() {
		}
	}

	private String username;
	private String message;
	private String linea;
	private Double lat;
	private Double lng;

	public Message() {
	}

	public Message(final String username, final String message) {
		this.username = username;
		this.message = message;
	}

	public Message(String username, String message, String linea, Double lat,
			Double lng) {
		super();
		this.username = username;
		this.message = message;
		this.linea = linea;
		this.lat = lat;
		this.lng = lng;
	}
	
	public Message(PojoTraking pojoTraking, String mng) {
		super();
		this.username = pojoTraking.getUsuario();
		this.message = mng;
		this.linea = pojoTraking.getLinea();
		this.lat = pojoTraking.getLat();
		this.lng = pojoTraking.getLng();
	}

	public Message(PojoTraking pojoTraking) {
		super();
		this.username = pojoTraking.getUsuario();
		this.message = pojoTraking.getLinea();
		this.linea = pojoTraking.getLinea();
		this.lat = pojoTraking.getLat();
		this.lng = pojoTraking.getLng();
	}

	public String getMessage() {
		return message;
	}

	public String getUsername() {
		return username;
	}

	public void setMessage(final String message) {
		this.message = message;
	}

	public void setUsername(final String username) {
		this.username = username;
	}

	public String getLinea() {
		return linea;
	}

	public void setLinea(String linea) {
		this.linea = linea;
	}

	public Double getLat() {
		return lat;
	}

	public void setLat(Double lat) {
		this.lat = lat;
	}

	public Double getLng() {
		return lng;
	}

	public void setLng(Double lng) {
		this.lng = lng;
	}

	@Override
	public String toString() {
		return "Mensaje [username=" + username + ", message=" + message + "]";
	}

}